<template>
  <div class="app">
    <a-breadcrumb style="margin-bottom: 10px">
      <a-breadcrumb-item>重点人群/自主申报</a-breadcrumb-item>
    </a-breadcrumb>
    <a-row class="pd20 mt20">
      <a-col :span="6">
        申报姓名：
        <a-input placeholder="请输入" v-model="queryForm.name" />
      </a-col>
      <a-col :span="6">
        联系方式：
        <a-input placeholder="请输入" v-model="queryForm.phone" />
      </a-col>
      <a-col :span="6">
        申报位置：
        <a-input placeholder="请输入" v-model="queryForm.reportLocation" />
      </a-col>
      <a-col :span="6">
        外出情况：
        <a-input placeholder="请输入" v-model="queryForm.outCondition" />
      </a-col>
      <a-col :span="6">
        报到情况：
        <a-input placeholder="请输入" v-model="queryForm.registerCondition" />
      </a-col>
      <a-col :span="6">
        申报日期：
        <a-date-picker
          v-model="queryForm.registerTime"
          placeholder="请选择"
          format="YYYY-MM-DD"
        />
      </a-col>
      <a-col :span="8">
        创建时间范围：
        <a-range-picker
          style="width: 350px"
          @change="onChange"
          v-model="dateRange"
          :placeholder="['开始日期', '结束日期']"
        />
      </a-col>
      <a-col :span="4">
        <a-button type="primary" @click="getList"> 查询 </a-button>&emsp;&nbsp;
        <a-button @click="cleanQuery">重置</a-button>
      </a-col>
    </a-row>
    <a-row class="pd20 mt20">
      <div class="btn_box">
        <div>自主申报</div>
        <a-button
          type="primary"
          @click="addFn"
          v-hasPermi="['zhonghe:zzsb:add']"
          >添加</a-button
        >
      </div>
      <a-spin :spinning="loading">
        <a-table
          :scroll="{ x: 500 }"
          :columns="columns"
          :data-source="tableData"
          :pagination="false"
        >
          <template slot="phone" slot-scope="text, record">
            <div class="phone-cell">
              <span>{{
                record.showPhone ? text : $desensitizePhone(text)
              }}</span>
              <a-icon
                :type="record.showPhone ? 'eye-invisible' : 'eye'"
                @click.stop="togglePhoneVisibility(record)"
              />
            </div>
          </template>
          <span slot="customTitle"><a-icon type="smile-o" /> Name</span>
          <span slot="action" slot-scope="text, record">
            <a
              @click="editFn(text, 'view')"
              v-hasPermi="['zhonghe:zdrqlxgl:edit']"
              >查看</a
            >
            <a-divider type="vertical" />
            <a
              @click="editFn(text, 'edit')"
              v-hasPermi="['zhonghe:zdrqxx:edit']"
              >修改</a
            >
            <a-divider type="vertical" />
            <a
              @click="delFn(text, record)"
              v-hasPermi="['zhonghe:zzsb:del']"
              class="red"
              >删除</a
            >
          </span>
        </a-table>
        <div class="page_box">
          <a-pagination
            v-model="queryForm.offset"
            :page-size-options="['20', '30', '40', '50']"
            :total="total"
            show-size-changer
            :page-size="queryForm.current"
            @showSizeChange="onShowSizeChange"
            @change="pageChange"
            :show-total="(total) => `共 ${total} 条`"
          ></a-pagination>
        </div>
      </a-spin>
    </a-row>
    <a-modal
      title="申报信息配置"
      :visible="dialogVisible"
      width="38%"
      ok-text="确认"
      cancel-text="取消"
      @cancel="dialogVisible = false"
      @ok="putDict"
    >
      <a-form :form="form">
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="姓名"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'name',
              { rules: [{ required: true, message: '请输入姓名' }] },
            ]"
            placeholder="请输入姓名"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="联系方式"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'phone',
              { rules: [{ required: true, message: '请输入联系方式' }] },
            ]"
            placeholder="请输入联系方式"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="申报位置"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'reportLocation',
              { rules: [{ required: true, message: '请输入申报位置' }] },
            ]"
            placeholder="请输入申报位置"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="外出情况"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'outCondition',
              { rules: [{ required: true, message: '请输入外出情况' }] },
            ]"
            placeholder="请输入外出情况"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="报到情况"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'registerCondition',
              { rules: [{ required: true, message: '请输入报到情况' }] },
            ]"
            placeholder="请输入报到情况"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="申报日期"
        >
          <a-date-picker
            style="width: 100%"
            :disabled="ModalType == 'view'"
            value-format="YYYY-MM-DD HH:mm:ss"
            show-time
            v-decorator="[
              'registerTime',
              { rules: [{ required: true, message: '请选择申报日期' }] },
            ]"
            placeholder="请选择申报日期"
            format="YYYY-MM-DD HH:mm:ss"
          />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import { Modal } from "ant-design-vue";
import { message } from "ant-design-vue";
import dayjs from "dayjs";
export default {
  data() {
    return {
      ModalType: "",
      dateRange: [],
      loading: true,
      dialogVisible: false,
      queryForm: { current: 20, offset: 1 },
      total: 0,
      form: this.$form.createForm(this, { name: "coordinated" }),
      id: null,
      tableData: [],
      columns: [
        // { title: '序号' },
        {
          title: "申报ID",
          dataIndex: "id",
          key: "id",
        },
        {
          title: "申报姓名",
          dataIndex: "name",
          key: "name",
        },
        {
          title: "联系方式",
          dataIndex: "phone",
          key: "phone",
          scopedSlots: { customRender: "phone" },
          width: 150,
        },
        {
          title: "申报位置",
          dataIndex: "reportLocation",
          key: "reportLocation",
        },
        {
          title: "外出情况",
          dataIndex: "outCondition",
          key: "outCondition",
        },
        {
          title: "报到情况",
          dataIndex: "registerCondition",
          key: "registerCondition",
        },
        {
          title: "申报日期",
          dataIndex: "registerTime",
          key: "registerTime",
        },
        {
          title: "最近更新时间",
          dataIndex: "updateTime",
          key: "updateTime",
        },
        {
          title: "创建时间",
          dataIndex: "createTime",
          key: "createTime",
        },
        {
          title: "操作",
          key: "action",
          width: 150,
          scopedSlots: { customRender: "action" },
        },
      ],
      formItemLayout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 19 },
      },
      formTailLayout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 19, offset: 4 },
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      const url = "/government-platform-center/autoRegister/queryByPage";
      // if (this.queryForm.registerTime) {
      //   this.queryForm.registerTime = dayjs(this.queryForm.registerTime).format(
      //     "YYYY-MM-DD"
      //   );
      // }
      const params = this.queryForm;
      this.$axios
        .get(url, {
          params: params,
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.tableData = res.data.data.map((item) => {
              item["showPhone"] = false;
              return item;
            });
            this.total = res.data.total;
            this.loading = false;
          }
        });
    },
    cleanQuery() {
      this.queryForm = { current: 20, offset: 1 };
      this.dateRange = [];
      this.getList();
    },
    // 分页
    onShowSizeChange(current, pageSize) {
      this.queryForm.current = pageSize;
      this.getList();
    },
    pageChange(current) {
      this.queryForm.offset = current;
      this.getList();
    },
    delFn(text, record) {
      const that = this;
      Modal.confirm({
        title: "确认删除",
        content: "您确定要删除这项内容吗？",
        okText: "确认",
        cancelText: "取消",
        onOk() {
          const url = `/government-platform-center/autoRegister/deleteById`;
          const form = { id: text.id };
          that.$axios
            .post(url, form, {
              headers: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
            })
            .then((res) => {
              if (res.data.code == 0) {
                that.getList();
                message.success("操作成功");
              }
            });
        },
        onCancel() {
          // 在此处理取消逻辑
          console.log("删除已取消");
        },
      });
    },
    goData(text, record) {
      this.$router.push({
        path: "/dataDict",
        query: { dictType: text.dictType, id: text.id },
      });
    },
    addFn() {
      this.dialogVisible = true;
      this.id = null;
      this.ModalType = "add";
      this.form.resetFields();
    },
    editFn(text, type) {
      this.ModalType = type;
      this.dialogVisible = true;
      const url = `/government-platform-center/autoRegister/detail?id=${text.id}`;
      this.$axios.get(url).then((res) => {
        if (res.data.code == 0) {
          this.form.setFieldsValue(res.data.data);
          this.id = res.data.data.id;
        }
      });
    },
    putDict() {
      this.form.validateFields((err, values) => {
        if (err) {
          return;
        }
        if (!this.id) {
          const url = "/government-platform-center/autoRegister/add";
          this.$axios.post(url, values).then((res) => {
            if (res.data.code == 0) {
              this.getList();
              this.reSetForm();
              this.dialogVisible = false;
              message.success("操作成功");
            }
          });
        } else {
          const form = { ...values, id: this.id };
          const url = "/government-platform-center/autoRegister/edit";
          this.$axios.post(url, form).then((res) => {
            if (res.data.code == 0) {
              this.getList();
              this.reSetForm();
              this.dialogVisible = false;
              message.success("操作成功");
            }
          });
        }
      });
    },
    reSetForm() {
      // this.form = { dictName: null, dictType: null, remark: null }
    }, // 处理日期范围变化
    onChange(dates, dateStrings) {
      if (dates) {
        this.queryForm.startTime = dateStrings[0];
        this.queryForm.endTime = dateStrings[1];
      } else {
        this.queryForm.startTime = null;
        this.queryForm.endTime = null;
      }
    },
    togglePhoneVisibility(record) {
      this.$set(record, "showPhone", !record.showPhone);
    },
  },
};
</script>

<style lang="scss" scoped>
.ant-row {
  border-radius: 5px;

  background: #fff;
  color: #000;
}

v-deep {
  .ant-table-thead > tr > th {
    color: #000;
    font-weight: bold;
  }
  .ant-table-tbody > tr > td {
    color: #000;
  }
  .el-table thead {
    color: black;
    font-weight: bold;
    background: #fafafa;
  }
  .el-table th.el-table__cell {
    background: #fafafa;
  }
}

.ant-col {
  margin: 10px 0;
}

.ant-select {
  width: 60%;
}

.ant-input {
  width: 60%;
}

@media screen and (max-width: 1300px) {
  .el-input {
    width: 70%;
  }

  .el-select {
    width: 70%;
  }

  .app {
    width: calc(100% - 50px);
  }
}

@media screen and (max-width: 1100px) {
  .el-input {
    width: 90%;
  }

  .el-select {
    width: 90%;
  }
}
</style>
